package org.xtb.CS_2;

import java.io.*;
import java.net.*;
import java.util.Objects;

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 8888);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

            while (true) {
                // 显示菜单
                System.out.println("==============================");
                System.out.println("个人通讯录系统");
                System.out.println("==============================");
                System.out.println("请选择操作：");
                System.out.println("1. 添加联系人");
                System.out.println("2. 修改联系人");
                System.out.println("3. 删除联系人");
                System.out.println("4. 查看联系人");
                System.out.println("5. 退出");
                System.out.print("请输入选项号码：");

                int choice = Integer.parseInt(consoleReader.readLine());
                StringBuilder response = new StringBuilder();
                String next ;

                switch (choice) {
                    case 1:
                        // 发送添加联系人请求
                        out.println("添加联系人");
                        System.out.println("请输入联系人姓名：");
                        String name = consoleReader.readLine();
                        out.println(name);
                        System.out.println("请输入联系人住址：");
                        String address = consoleReader.readLine();
                        out.println(address);
                        System.out.println("请输入联系人电话：");
                        String phone = consoleReader.readLine();
                        out.println(phone);

                        // 接收服务器响应
                        response.append(in.readLine());
                        System.out.println("服务器响应：" + response);
                        response.setLength(0);
                        break;
                    case 2:
                        // 发送修改联系人请求
                        String str1;
                        do{
                            out.println("修改联系人");
                            System.out.println(in.readLine());
                            name = consoleReader.readLine();
                            out.println(name);
                            str1= in.readLine();
                        } while (Objects.equals(str1, "联系人不存在！"));
                        System.out.println(str1);
                        String newName = consoleReader.readLine();
                        out.println(newName);
                        System.out.println(in.readLine());
                        address = consoleReader.readLine();
                        out.println(address);
                        System.out.println(in.readLine());
                        phone = consoleReader.readLine();
                        out.println(phone);
                        System.out.println(in.readLine());
                        break;
                    case 3:
                        // 发送删除联系人请求
                        out.println("删除联系人");
                        System.out.println("请输入要删除的联系人姓名：");
                        name = consoleReader.readLine();
                        out.println(name);

                        // 接收服务器响应
                        String str;
                        while ((str =in.readLine())!=null){
                            if("列表结尾".equals(str))break;
                            System.out.println(str);
                        }
                        break;
                    case 4:
                        // 发送查看联系人请求
                        out.println("查看联系人");

                        // 接收服务器响应
                        while ((str =in.readLine())!=null){
                            if("列表结尾".equals(str))break;
                            System.out.println(str);
                        }

                        break;
                    case 5:
                        System.out.println("退出程序");
                        in.close();
                        out.close();
                        consoleReader.close();
                        socket.close();
                        System.exit(0);
                    default:
                        System.out.println("无效的选项，请重新输入");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}